//
//  LHProvinceView.m
//  Knight_Tour
//
//  Created by Bruce_Lin on 15/5/13.
//  Copyright (c) 2015年 TouchOne. All rights reserved.
//

#import "LHProvinceView.h"
#import "LHProvinceCollectionViewCell.h"
#import "LHNetworkHandle.h"
#import "LHProvinceModel.h"
#import "UIImageView+WebCache.h"
#import "UIScrollView+MJRefresh.h"

@implementation LHProvinceView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {

        [self creatView];
    }
    return self;
}

- (void)creatView
{
    UICollectionViewFlowLayout *fly = [[UICollectionViewFlowLayout alloc]init];
    [fly setItemSize:CGSizeMake(LHWidth/3 - 20, (LHheight - 64) / 4)];
    [fly setSectionInset:UIEdgeInsetsMake(5, 10, 5, 10)];
    
    self.provinceCollectonView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:fly];
    _provinceCollectonView.delegate = self;
    _provinceCollectonView.dataSource = self;
    
    //添加头部下拉刷新
    
    [_provinceCollectonView addHeaderWithTarget:self action:@selector(fresh)];
    _provinceCollectonView.headerRefreshingText = @"正在努力刷新";
    
    self.provinceCollectonView.backgroundColor = [UIColor colorWithRed:0.871 green:0.962 blue:0.965 alpha:1.000];
    [self addSubview:_provinceCollectonView];
    [_provinceCollectonView registerClass:[LHProvinceCollectionViewCell class] forCellWithReuseIdentifier:@"provinceReuse"];
    [fly release];
    [_provinceCollectonView release];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _proModelArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LHProvinceCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"provinceReuse" forIndexPath:indexPath];
    [cell.picView sd_setImageWithURL:[NSURL URLWithString:[self.proModelArr[indexPath.item]pic]] placeholderImage:[UIImage imageNamed:@"word4"]];
     cell.privinceName.text = [self.proModelArr[indexPath.item] namec];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate jumpToCityWithIndexPath:indexPath];
}

/**
 *  下拉刷新响应方法
 */
- (void)fresh
{
    [_provinceCollectonView reloadData];
    [_provinceCollectonView headerEndRefreshing];
}



@end
